package cn.tedu.suppermemory.service.impl;

import cn.tedu.suppermemory.ex.ServiceException;
import cn.tedu.suppermemory.mapper.ClassMapper;
import cn.tedu.suppermemory.pojo.dto.ClassAddDTO;
import cn.tedu.suppermemory.pojo.dto.ClassUpdateDTO;
import cn.tedu.suppermemory.pojo.entity.Class;
import cn.tedu.suppermemory.pojo.vo.ClassListItemVO;
import cn.tedu.suppermemory.pojo.vo.ClassStandardVO;
import cn.tedu.suppermemory.service.IClassService;
import cn.tedu.suppermemory.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ClassServiceImpl implements IClassService {
    @Autowired
    private ClassMapper classMapper;
    @Override
    public void addNew(ClassAddDTO classAddDTO) {
        log.debug("开始处理【添加班级】业务，参数{}",classAddDTO);
        String name=classAddDTO.getName();
        int count=classMapper.countByName(name);
        if(count>0){
            String message="添加班级失败，该班级已经存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Class clsaay=new Class();
        BeanUtils.copyProperties(classAddDTO,clsaay);
        int rows=classMapper.insert(clsaay);
        if(rows!=1){
            String message = "添加班级失败，服务器忙，请稍后再尝试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据id删除班级业务】，参数：",id);
        Object queryResult=classMapper.getStandardById(id);
        if(queryResult==null){
            String message = "删除班级失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        log.debug("即将执行删除数据，参数：{}", id);
        int rows=classMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除班级失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, "删除班级失败，服务器忙，请稍后再次尝试！");
        }

    }

    @Override
    public void updateInfoClass(Long id, ClassUpdateDTO classUpdateDTO) {
        Class classy=new Class();
        BeanUtils.copyProperties(classUpdateDTO,classy);
        classy.setId(id);
        int rows=classMapper.update(classy);
        if (rows != 1) {
            String message = "修改班级详情失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }

    }


    @Override
    public List<ClassListItemVO> list() {
        log.debug("开始处理【查询班级列表】的业务，无参数");
        List<ClassListItemVO> list=classMapper.list();
        return list;
    }

    @Override
    public ClassStandardVO getStandardById(Long id) {
        ClassStandardVO classStandardVO= classMapper.getStandardById(id);
        if(classStandardVO==null){
            String message = "查询班级详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return classStandardVO;

    }
}
